<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24"  v-if="userInfo.userType == 1">
            <a-form-model-item label="分站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="siteId">
              <a-input v-model="model.siteId" placeholder="请选择分站"  ></a-input>
            </a-form-model-item>
          </a-col> 
          <a-col :span="24">
            <a-form-model-item label="信息类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parentType">
              <!-- <j-category-select v-model="model.parentType" pcode="B03" placeholder="请选择信息类型父"  /> -->
              <a-radio-group @change="handleParentTypeInput(model.parentType, ()=>{})" v-model="model.parentType">
                <a-radio-button v-for="(item, key) in parentTypeOptions" :key="key" :value="item.code">{{ item.title }}</a-radio-button>
              </a-radio-group>
              <a-radio-group @change="handleChildrenType(model.childrenType)" v-model="model.childrenType" style="right: -156.66px;">
                <a-radio v-for="(item, key) in childrenTypeOptions" :key="key" :value="item.code">{{ item.title }}</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="信息类型子" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="childrenType">
              <a-input-number v-model="model.childrenType" placeholder="请输入信息类型子" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="信息名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入信息名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-textarea v-model="model.content" rows="4" placeholder="请输入内容" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="title == '详情' && model.images">
            <a-form-model-item label="配图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="images">
              <j-image-upload biz-path="laile2/information" isMultiple :number=9 v-model="model.images" ></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
        
      </a-form-model>
      <span v-if="title=='详情' && permissionData.informationReplyView">
        <a-table
          ref="replyTable"
          :loading="replyTableData.loading"
          :columns="replyTableData.columns"
          :dataSource="replyTableData.dataSource"
          :maxHeight="300"
          :rowNumber="true"/>
      </span>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { FormTypes, VALIDATE_NO_PASSED, getRefPromise, validateFormModelAndTables } from '@/utils/JEditableTableUtil'
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { mapGetters } from 'vuex'

  export default {
    name: 'InformationForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      title: String,
    },
    data () {
      return {
        model:{},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           parentType: [
              { required: true, message: '请选择信息类型!'},
           ],
           name: [
              { required: true, message: '请输入信息名称!'},
           ],
           siteId: [
              { required: true, message: '请选择分站!'},
           ],
        },
        // 客户信息
        replyTableData: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '回复人',
              key: 'createBy_dictText',
              width: '25%',
              type: FormTypes.normal,
              defaultValue: '',
            },
            {
              title: '回复内容',
              key: 'content',
              width: '75%',
              type: FormTypes.normal,
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/information/add",
          edit: "/information/edit",
          replyList: "/informationReply/list",
        },
        parentTypeOptions: [],
        childrenTypeOptions: [],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      ...mapGetters(["permissionData", "userInfo"]),
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model))
    },
    methods: {
      loadReply() {
        if (this.title!='详情') return
        this.replyTableData.loading = true
        console.log(this.model)
        getAction(this.url.replyList, {infoId: this.model.id}).then(res=>{
          this.replyTableData.loading = false
          if (res.success) this.replyTableData.dataSource = res.result.records
        })
      },
      handleChildrenType(childrenType) {
        this.model.childrenType = childrenType
      },
      handleParentTypeInput(parentType, callback) {
        if (!parentType) return
        this.model.parentType = parentType
        getAction("/sys/category/loadTreeData", {pcode: this.model.parentType}).then(res => {
          this.childrenTypeOptions = res.result
          delete this.model['childrenType']
          callback()
        })
      },
      loadParentType(callback) {
        getAction("/sys/category/loadTreeData", {pcode: 'B03'}).then(res=>{
          this.parentTypeOptions = res.result
          callback()
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.loadReply()
        let childrenType = this.model.childrenType
        this.loadParentType(() => {
          this.handleParentTypeInput(this.model.parentType, () => {
            this.handleChildrenType(childrenType)
          })
        });
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      handleCategoryChange(value,backObj){
         this.model = Object.assign(this.model, backObj);
      },
    }
  }
</script>